import { wxRequest } from '../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp();
Component({
  properties: {
    multiArray: {
      type: Array,
      value: [18, 16, 24]
    }, 
    disCode: String, 
  },
  data: {
    disCode: 441900122000, //默认东莞厚街
    disName: '厚街镇',
  },


  ready() { 
    var that = this 
    setTimeout(function() {  
      console.log(that.properties,'discode') 
      // that.originalArea(that.properties.disCode, that.data.disName); 
      that.setData({
        multiArray: [that.data.Province, that.data.City, that.data.Area],
        multiIndex: [that.data.provinceIndex || 18, that.data.cityIndex || 16, that.data.areaIndex || 24],//默认广东省东莞市厚街镇 
      })
    }, 1000)
  },

  methods: {
    //点击确定
    bindMultiPickerChange: function(e) {
      const val = e.detail.value
      var that = this;
      this.setData({
        multiArray: [that.data.Province, that.data.City, that.data.Area],
        multiIndex: val,
        selectCity: false
      }) 
      var pramats = {
        disCode: this.data.disCode,
        disName: this.data.disName
      }
      this.triggerEvent("confirm", pramats)
    },
    //修改列
    bindMultiPickerColumnChange: function(e) {
      var that = this;
      var index = e.detail.value
      // console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
      switch (e.detail.column) {
        case 0:
          var pid = that.data.Province[index].id;
          that.setData({
            selectCity: true, //标记是否通过滚动
            multiIndex: [index, 0, 0], //高亮
            province: index
          })
          that.getCity(pid); //实例化城市选项
          return;
        case 1:
          var pid = that.data.City[index].id;

          that.setData({
            multiIndex: [that.data.province || that.data.multiIndex[0], index || that.data.multiIndex[1], 0], //初始化选择值，如果有拉动省份则读取省份，没有读取默认
            selectCity: true, // 标记是否通过滚动
            city: index,
            cityCode: that.data.City[index].id, //没选中三级时，给第一个id
            cityName: that.data.City[index].name, ///没选中三级时，给第一个name
          })
          that.getArea(pid); //实例化地区
          return;
        case 2: 
          var pid = that.data.Area[index].id;
          that.setData({
            disCode: that.data.Area[index].id, //选择三级id
            disName: that.data.Area[index].name, //选择三级id
            multiIndex: [that.data.province || that.data.multiIndex[0], that.data.city || that.data.multiIndex[1], index] //初始化选择值，如果有拉动省份则读取省份和市，没有读取默认
          })
          return;
      }
    },
    //获取省份列表数据
    getProvince: function() {
      var that = this
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: "post",
        data: {
          pid: 0
        }
      }, function(res) { 
        var resData = res.data;
        //回调处理
        if (that.data.originalAreaData) {
          for (var i=0; i <resData.length; i++) {
            if (resData[i].id == that.data.originalAreaData.provinceCode) {
              that.setData({
                provinceIndex: i,
                multiIndex: [that.data.provinceIndex, 0, 0],
              })
            }
          }
        }

        that.setData({
          Province: resData 
        })
      })
    },

    //获取市区列表数据
    getCity: function(pid) {
      var that = this
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: "post",
        data: {
          pid: pid
        }
      }, function(res) {
        
        var resData = res.data;
        //回调处理
        if (that.data.originalAreaData) {
          for (var i = 0; i <resData.length; i++) {
            if (resData[i].id == that.data.originalAreaData.cityCode) {
              that.setData({
                cityIndex: i
              })
            }
          }
        }

        that.setData({
          City: resData, 
          multiIndex: [that.data.provinceIndex, that.data.cityIndex,0],
        })

        if (that.data.selectCity) {
          that.getArea(resData[0].id); //更新地区信息 
        }
      })
    },

    //获取地区列表数据
    getArea: function (pid, discode, disname) {
      var that = this
      _wxRequest.wxAjax({
        url: app.globalData.apiUrl + '/sys/district/findByPid',
        method: "post",
        data: {
          pid: pid
        }
      }, function(res) {
        var resData = res.data; 
        //回调处理
        if (that.data.originalAreaData) {
          for (var i = 0 ;i<resData.length;i++) {
            if (resData[i].id == that.data.originalAreaData.areaCode) {
              that.setData({
                areaIndex: i
              })
            }
          }
        }

        that.setData({
          Area: resData,
          multiArray: [that.data.Province, that.data.City, resData],
          multiIndex: [that.data.provinceIndex, that.data.cityIndex, that.data.areaIndex],
          disCode: resData[0].id, //没选中三级时，给第一个id
          disName: resData[0].name, ///没选中三级时，给第一个name
        })
        //不选择地址情况下
        if (discode) {
          that.setData({
            disCode: discode,
            disName: disname
          })
        }
        console.log(that.data.multiIndex,'multiIndex')
        console.log(that.data.multiArray, 'multiArray')
      })
    },



    //回调函数
    // originalArea: function(id,name) {
    //   var that = this
    //   _wxRequest.wxAjax({
    //     url: app.globalData.apiUrl + '/district/findByThreeLevelCode',
    //     method: "post",
    //     data: {
    //       id: id
    //     }
    //   }, function(res) {
    //     if (res.data.code = '0000') {
    //       var resData = res.data;
    //       that.setData({
    //         originalAreaData: resData
    //       }) 
    //       that.getProvince();
    //       that.getCity(resData.provinceCode);
    //       that.getArea(resData.cityCode, id,name);
    //     }
    //   })
    // },

  }



})